// pages/index/index.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isVoice: false,//控制DOM切换语音样式
    imgDomain: app.globalData.imgDomain,
    array: []
  },
  /**
   * 事件处理函数
   */
  toPageSearch: function () {
    wx.navigateTo({
      url: '../search/index'
    })
  },
  /*录音请求事件开始*/
  recorderStart: function() {
    this.setData({
      isVoice: true
    });
    wx.startRecord({
      success: function(res) {
        wx.showLoading({
          title: '加载中',
          icon: 'loading'
        });
        console.dir(res);
        wx.uploadFile({
          url: 'https://aisearch.api.lianjia.com/api/1011720001/search?gbCode=110000',
          filePath: res.tempFilePath,
          name: 'file',
          header: {
            'token':'lTdWj91hGr'
          },
          success: function(res) {
            wx.hideLoading();
            console.log('begin');
          },
          complete: function() {
            wx.hideLoading();
          }
        })
      }
    })    
  },
  /*录音结束事件*/
  recorderEnd: function() {
    this.setData({
      isVoice: false
    });
    wx.stopRecord();
  },
  /*
  * 模拟下拉分页加载数据事件
  */
  lower: function() {
    var selfArr = this.data.array;
    for(let i=0;i<10;i++) {
      selfArr.push({
        "houseDelCode": "101102775822",
        "buildEndDate": 441734400000,
        "bizcircleName": "东直门",
        "delType": "1",
        "isInResourcePool": "0",
        "cityId": "110000",
        "floorId": "1114028853090",
        "communityName": "香河园北里",
        "houseStatus": "",
        "priceChange": "-1",
        "priceUnit": "89315.796875",
        "priceTotal": "530",
        "hurryStatus": null,
        "orientation": ["100500000003", "100500000007"],
        "floorLevel": "4",
        "floor": "5",
        "floorTotal": "6",
        "titleImage": "/110000-inspection/c702969a-768f-4685-9c2d-1944bee6af07.JPG",
        "orgCodeMaintain": ["A13F312", "A11343", "A11C25", "A12N96", "A12P30", "A13A53", "A13B23", "A13F31", "A11302", "A12W13", "A12Z85", "A13D68", "A13F03", "A13F211", "A13F212", "XN31225", "A13F102", "XN31195"],
        "createTime": 1523843487000,
        "bedroomNum": 2,
        "hallNum": 1,
        "bathroomNum": 1,
        "kitchenNum": 1,
        "paymentMode": "307500000001",
        "subwayDistance": 0,
        "subwayStationId": ["20607", "43143370"],
        "subwayLineId": ["649", "652"],
        "resourcePoolTime": null,
        "resourcePoolReason": null,
        "holderUcid": "1000000020135422",
        "holderName": "林能福",
        "houseLabels": null,
        "surfaceImage": null
      })
    }
    if (selfArr.length >= 100) {
      wx.showToast({
        title: '我也是有底线的',
        icon: 'success',
        duration: 500
      });
      return false;
    }else {
      wx.showLoading({
        title: '加载中',
        icon: 'loading'
      })
      setTimeout(() => {
        this.setData({
          array: selfArr
        });
        wx.hideLoading();
      }, 1500)
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _self = this;
    //判断storage是否有数据，有就替换当前列表，否则请求一次
    if (typeof wx.getStorageSync('lists') == 'string') {
      _self.setData({
        array: JSON.parse(wx.getStorageSync('lists'))
      })
    } else {
      wx.request({
        url: 'https://aisearch.api.lianjia.com/api/weixin/search',
        method: 'POST',
        header: {
          "token": "lTdWj91hGr",
          "Content-Type": "application/json"
        },
        data: {
          "token": "xiaochengxu",
          "query": ''
        },
        success: function (res) {
          let datas = [];
          res.data.datas.forEach(function (item) {
            datas.push({
              ...item,
              priceTotal: utils.priceChanges(item.priceTotal)
            })
          })
          console.dir(datas);
          _self.setData({
            array: datas
          });
        },
        complete: function () {
          wx.hideLoading();
        }
      })
    }
  }
})